package com.lwt.yygh.hosp.controller.api;

import com.alibaba.excel.util.StringUtils;
import com.lwt.yygh.common.exception.YyghException;
import com.lwt.yygh.common.helper.HttpRequestHelper;
import com.lwt.yygh.common.result.Result;
import com.lwt.yygh.common.result.ResultCodeEnum;
import com.lwt.yygh.common.utils.MD5;
import com.lwt.yygh.hosp.service.DepartmentService;
import com.lwt.yygh.hosp.service.HospitalService;
import com.lwt.yygh.hosp.service.HospitalSetService;
import com.lwt.yygh.hosp.service.ScheduleService;
import com.lwt.yygh.model.hosp.Department;
import com.lwt.yygh.model.hosp.Hospital;
import com.lwt.yygh.model.hosp.Schedule;
import com.lwt.yygh.vo.hosp.DepartmentQueryVo;
import com.lwt.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author Weton Li
 * @Date 2021/4/21 16:03
 */

@RestController
@RequestMapping("/api/hosp")

public class ApiController {
    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;
    /**
     * 上传医院接口方法
     * post形式提交给服务器的数据通过request获取到请求参数。
     *
     * @param httpServletRequest
     * @return
     */
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest httpServletRequest) {
// 接收 医院模拟接口系统传来的数据
        Map<String, String[]> requestMap = httpServletRequest.getParameterMap();
        // 工具类方法
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap); //签名正进行MD5加密

        // 1.获取医院系统传递过来的签名
        String hospSign = (String) paramMap.get("sign"); //签名已进行MD5加密

        System.out.println("------------" + hospSign + "------------");

        // 2.根据传递过来的医院编码，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        String signKeyMd5 = MD5.encrypt(signKey);
        System.out.println("------------" + signKeyMd5 + "------------");
        // 将模拟接口管理传来的加密签名与数据库彤医通中的加密签名进行比较
        if (!hospSign.equalsIgnoreCase(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        // 通过base64编码在传输时加号变空格
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);

        hospitalService.save(paramMap);
        return Result.ok();
    }

    /**
     * 医院一条bean信息展示
     * @param httpServletRequest
     * @return
     */
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest httpServletRequest){
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        String sign = (String) paramMap.get("sign"); // 医院传给服务器的签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);

        if (!sign.equalsIgnoreCase(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        Hospital hospital = hospitalService.getHospitalByHoscode(hoscode);
        System.out.println("getHospital返回json数据");
        return Result.ok(hospital);
    }

    // 科室

    /**
     * 上传科室接口 到mongodb中
     * @param httpServletRequest
     * @return
     */
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest httpServletRequest){

        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        String hoscode = (String) paramMap.get("hoscode");
        String sign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        if (!sign.equalsIgnoreCase(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.saveDept(paramMap);
        System.out.println("saveDepartment返回json数据");

        return Result.ok();
    }

    /**
     * mongo分页查询科室
     * @param httpServletRequest
     * @return
     */
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest httpServletRequest){
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);


        String hoscode = (String) paramMap.get("hoscode"); // 获取医院编号
        int page = StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit"))?1:Integer.parseInt((String)paramMap.get("limit"));

        // 签名校验

        // 分页
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        
        Page<Department> pageModel = departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 删除科室接口
     * @param httpServletRequest
     * @return
     */
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest httpServletRequest){
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        // 签名校验

        departmentService.removeDept(hoscode,depcode);
        return Result.ok();
    }

    // 排班

    /**
     * 上传排班到mongo
     * @param httpServletRequest
     * @return
     */
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest httpServletRequest){
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        // 签名校验

        scheduleService.saveSchedu(paramMap);
        return Result.ok();
    }

    /**
     * 分页查询排班
     * @param httpServletRequest
     * @return
     */
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest httpServletRequest){
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        String hoscode = (String) paramMap.get("hoscode");
        String deptcode = (String) paramMap.get("deptcode");

        // 签名校验

        int page = StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit"))?1:Integer.parseInt((String)paramMap.get("limit"));

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        // 因为业务功能强大，为了兼容多家医院，所以不同医院的医院编号和部门编号需要独立出来，以便对不同家医院分页
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(deptcode);
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 删除排班
     * @param httpServletRequest
     * @return
     */
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest httpServletRequest){
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        scheduleService.removeSchedu(hoscode,hosScheduleId);
        return Result.ok();
    }
}
